<?php
class Fphoto_IndexController extends Core_Controller_Action_Standard
{
  public function downloadImageAction(){
  	$photo_id = $this->_getParam('photo_id');
  	$photo = Engine_Api::_()->getItem('album_photo', $photo_id);
  	if($photo)
  		$src = $photo->getPhotoUrl();
  	else 
  		die("NO FILE HERE");
  	$src = explode('?', $src);
  	$src = $_SERVER["DOCUMENT_ROOT"] .$src[0];  	
    $file_extension = strtolower(substr(strrchr($src,"."),1));
    switch ($file_extension) {
        case "pdf": $ctype="application/pdf"; break;
        case "exe": $ctype="application/octet-stream"; break;
        case "zip": $ctype="application/zip"; break;
        case "doc": $ctype="application/msword"; break;
        case "xls": $ctype="application/vnd.ms-excel"; break;
        case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
        case "gif": $ctype="image/gif"; break;
        case "png": $ctype="image/png"; break;
        case "jpe": case "jpeg":
        case "jpg": $ctype="image/jpg"; break;
        default: $ctype="application/force-download";
     }

    if (!file_exists($src)) {
         die("NO FILE HERE");
    }
	 header("Pragma: public");
     header("Expires: 0");
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
     header("Cache-Control: private",false);
     header("Content-Type: $ctype");
     header("Content-Disposition: attachment; filename=\"".basename($src)."\";");
     header("Content-Transfer-Encoding: binary");
     header("Content-Length: ".@filesize($src));
     set_time_limit(0);
     @readfile("$src") or die("File not found.");
	exit;
  }
  public function showAction(){
  	if( !$this->_helper->requireAuth()->setAuthParams('album', null, 'view')->isValid() ) return;
  	if( 0 !== ($user_id = (int) $this->_getParam('user_id')) &&
        null !== ($user = Engine_Api::_()->getItem('user', $user_id)) )        
    {
      Engine_Api::_()->core()->setSubject($user);
    }
    if( !$this->_helper->requireSubject('user')->isValid() ) return;
    // Render
    $content = $this->_helper->content->setEnabled();
    
 	$type = $this->_getParam('type', '');   
 	$subject = $this->_getParam('subject', '');
 	if($type == 'viewall' && $subject == 'albums'){
 		// Get paginator
	    $this->view->paginator = $paginator = Engine_Api::_()->getItemTable('album')
	      ->getAlbumPaginator(array('owner' => $subject, 'search' => 1));
	
	    // Set item count per page and current page number
	    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', $this->_getParam('itemCountPerPage', 99999999)));
 	}else{  
		$content->setNoRender();
 	}    
  }
}